package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.search.domain.DestinationEs;
import cn.wolfcode.wolf2w.search.domain.StrategyEs;
import cn.wolfcode.wolf2w.search.domain.TravelEs;
import cn.wolfcode.wolf2w.search.domain.UserInfoEs;
import cn.wolfcode.wolf2w.search.query.SearchQueryObject;
import cn.wolfcode.wolf2w.search.service.ISearchService;
import cn.wolfcode.wolf2w.search.vo.SearchResultVO;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.ParamMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 甘静斌 on 2020-08-20 19:37
 */
@RestController
public class SearchController {

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private ITravelService travelService;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private ISearchService searchService;

    @RequestMapping("/q")
    public Object search(SearchQueryObject qo) throws UnsupportedEncodingException {
        String kw = URLDecoder.decode(qo.getKeyword(), "utf-8");
        qo.setKeyword(kw);
        switch(qo.getType()){
            case SearchQueryObject.TYPE_DEST:
                return this.searchDest(qo);
            case SearchQueryObject.TYPE_STRATEGY:
                return this.searchStrategy(qo);
            case SearchQueryObject.TYPE_TRAVEL:
                return this.searchTravel(qo);
            case SearchQueryObject.TYPE_USER:
                return this.searchUser(qo);

            default:
                return this.searchAll(qo);
        }
    }


    //查询目的地
    private Object searchDest(SearchQueryObject qo){

        //查询keyword对应的目的地是否存在
        Destination dest = destinationService.queryByName(qo.getKeyword());
        SearchResultVO vo = new SearchResultVO();
        //如果存在,查询该目的地下所有攻略,游记  用户
        if (dest != null){
            List<Strategy> sts = strategyService.queryByDestId(dest.getId());
            List<Travel> ts = travelService.queryByDestId(dest.getId());
            List<UserInfo> us = userInfoService.queryByCity(dest.getName());
            for (Travel travel : ts) {
                UserInfo userInfo = userInfoService.get(travel.getUserId());
                travel.setAuthor(userInfo);
            }

            vo.setStrategys(sts);
            vo.setTravels(ts);
            vo.setUsers(us);
            vo.setTotal(sts.size() + ts.size() + us.size() + 0L);
        }

        //返回
        //result  total   strategys   travels  users
        Map<String, Object> map = new HashMap<>();
        map.put("result",vo );
        map.put("dest",dest );
        map.put("qo",qo );
        return JsonResult.success(map);
    }

    //搜索攻略
    private Object searchStrategy(SearchQueryObject qo){

        //攻略的全文搜索 + 高亮
        Page<Strategy> page = searchService.searchWithHighlight(StrategyEs.INDEX_NAME, StrategyEs.TYPE_NAME, Strategy.class,
                qo, "title", "subTitle", "summary");
        return JsonResult.success(ParamMap.newInstance().put("page",page ).put("qo",qo ));
    }

    //搜索游记
    private Object searchTravel(SearchQueryObject qo){
        Page<Travel> page = searchService.searchWithHighlight(TravelEs.INDEX_NAME, TravelEs.TYPE_NAME, Travel.class,
                qo, "title", "summary");
        return JsonResult.success(ParamMap.newInstance().put("page",page ).put("qo",qo ));
    }

    //搜索用户
    private Object searchUser(SearchQueryObject qo){

        Page<UserInfo> page = searchService.searchWithHighlight(UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME, UserInfo.class,
                qo, "city", "info");
        return JsonResult.success(ParamMap.newInstance().put("page",page ).put("qo",qo ));
    }

    //搜索全部
    private Object searchAll(SearchQueryObject qo){
        Page<Strategy> sts = searchService.searchWithHighlight(StrategyEs.INDEX_NAME, StrategyEs.TYPE_NAME, Strategy.class,
                qo, "title", "subTitle", "summary");
        Page<Travel> ts = searchService.searchWithHighlight(TravelEs.INDEX_NAME, TravelEs.TYPE_NAME, Travel.class,
                qo, "title", "summary");
        Page<UserInfo> us = searchService.searchWithHighlight(UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME, UserInfo.class,
                qo, "city", "info");
        Page<Destination> ds = searchService.searchWithHighlight(DestinationEs.INDEX_NAME, DestinationEs.TYPE_NAME, Destination.class,
                qo, "name", "info");
        SearchResultVO vo = new SearchResultVO();

        vo.setUsers(us.getContent());
        vo.setTravels(ts.getContent());
        vo.setDests(ds.getContent());
        vo.setStrategys(sts.getContent());
        vo.setTotal(us.getTotalElements() + ts.getTotalElements() + sts.getTotalElements() + ds.getTotalElements());
        Map<String, Object> map = new HashMap<>();
        map.put("result",vo );
        map.put("qo",qo );
        return JsonResult.success(map);
    }
}
